setwd("/home1/30/jc227089/SRE_inv/PBS")

#set up parameter space
sp<-c(0, 30)
dc<-c(0, 0.1, 0.5, 0.9)
K<-c(40, 100)
ld<-c(2, 5, 10, 15, 20)

script.file<-'/home1/30/jc227089/evo-dispersal/SRE_inv/SRE_inv_ParSpace.R'

for(ll in 1:length(ld)){	
	for(kk in 1:length(K)){
		for (jj in 1:length(dc)){
			for (ii in 1:length(sp)) {
				fid<-(ll-1)*length(K)*length(dc)*length(sp)+(kk-1)*length(dc)*length(sp)+(jj-1)*length(sp)+ii
				##create the sh file
				zz = file(paste("SREinv_ParSpace", fid,'.sh',sep=''),'w')
				cat('##################################\n',file=zz)
				cat('#!/bin/sh\n',file=zz)
				cat('cd $PBS_O_WORKDIR\n',file=zz)
				cat("R CMD BATCH --no-save --no-restore '--args rr=5 file.ID=",fid, " sp=", sp[ii],
					 " dc=", dc[jj], " K=", K[kk], " ld=", ld[ll], "' ", sep="", file=zz)
				cat(script.file, " ", paste("SREinv_ParSpace", fid,'.Rout',sep=''), "\n", sep="",file=zz)
				cat('##################################\n',file=zz)
				close(zz)
			
				#submit the job
				system(paste("qsub -m n ", paste("SREinv_ParSpace", fid,".sh",sep=""),sep=""))
			}
		}
	}
}
